package org.dxy.domain.model.entity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 替代料实体
 * @author dxy
 * @createTime 2025/9/13
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class SubstituteMaterial {
    
    /** 替代料ID */
    private Long id;
    
    /** MBOM项ID */
    private Long mbomItemId;
    
    /** 替代料物料ID */
    private Long substituteMaterialId;
    
    /** 替代比例 */
    private BigDecimal substituteRatio;
    
    /** 优先级 */
    private Integer priority;
    
    /** 有效期开始 */
    private LocalDateTime validFrom;
    
    /** 有效期结束 */
    private LocalDateTime validTo;
    
    /** 状态：DRAFT-草稿，PENDING_APPROVAL-待审批，ACTIVE-可用，REJECTED-拒绝，EXPIRED-已过期，OBSOLETED-废止 */
    private String status;
    
    /** 拒绝原因 */
    private String rejectReason;
    
    /**
     * 是否可以在MES投料中使用
     */
    public boolean isAvailableForMES() {
        return "ACTIVE".equals(status) && isWithinValidPeriod();
    }
    
    /**
     * 是否在有效期内
     */
    public boolean isWithinValidPeriod() {
        LocalDateTime now = LocalDateTime.now();
        return (validFrom == null || !now.isBefore(validFrom)) && 
               (validTo == null || !now.isAfter(validTo));
    }
    
    /**
     * 检查是否已过期
     */
    public boolean isExpired() {
        return validTo != null && LocalDateTime.now().isAfter(validTo);
    }
    
    /**
     * 更新替代料信息
     */
    public void update(BigDecimal substituteRatio, Integer priority, 
                      LocalDateTime validFrom, LocalDateTime validTo) {
        validateBusinessRule("DRAFT".equals(this.status), "只有草稿状态的替代料才能修改");
        validateBusinessRule(substituteRatio != null && substituteRatio.compareTo(BigDecimal.ZERO) > 0, 
                           "替代比例必须大于0");
        validateBusinessRule(priority != null && priority > 0, "优先级必须大于0");
        
        this.substituteRatio = substituteRatio;
        this.priority = priority;
        this.validFrom = validFrom;
        this.validTo = validTo;
    }
    
    /**
     * 审核通过
     */
    public void approve() {
        validateBusinessRule("PENDING_APPROVAL".equals(this.status), "只有待审批状态的替代料才能审核通过");
        this.status = "ACTIVE";
        this.rejectReason = null;
    }
    
    /**
     * 审核拒绝
     */
    public void reject(String rejectReason) {
        validateBusinessRule("PENDING_APPROVAL".equals(this.status), "只有待审批状态的替代料才能审核拒绝");
        validateBusinessRule(rejectReason != null && !rejectReason.trim().isEmpty(), "拒绝原因不能为空");
        
        this.status = "REJECTED";
        this.rejectReason = rejectReason;
    }
    
    /**
     * 提交审批
     */
    public void submitForApproval() {
        validateBusinessRule("DRAFT".equals(this.status), "只有草稿状态的替代料才能提交审批");
        this.status = "PENDING_APPROVAL";
    }
    
    /**
     * 标记为过期
     */
    public void markAsExpired() {
        validateBusinessRule(isExpired(), "替代料尚未过期");
        this.status = "EXPIRED";
    }
    
    /**
     * 废止
     */
    public void obsolete() {
        validateBusinessRule(!"OBSOLETED".equals(this.status), "替代料已经是废止状态");
        this.status = "OBSOLETED";
    }
    
    /**
     * 验证业务规则
     */
    private void validateBusinessRule(boolean condition, String message) {
        if (!condition) {
            throw new IllegalStateException(message);
        }
    }
}